import 'package:flutter/material.dart';
import 'package:sow_app/view/common/chip_filter.dart';
import 'package:sow_app/view/common/novel/novel_list_tile.dart';
import 'package:sow_app/view/library/classify/classify_page.dart';
import 'package:sow_app_core/sow_app_core.dart';
import 'package:sow_app_i18n/sow_app_i18n.dart';

import '../../../presenter/state_tag_novel_presenter.dart';

class TagNovelListPage extends StatefulWidget {
  const TagNovelListPage(
      {Key? key, required this.controller, required this.presenter})
      : super(key: key);
  static const routeName = '${ClassifyPage.routeName}/tag_novel_page';
  final TagNovelController controller;
  final StateTagNovelPresenter presenter;

  @override
  State<TagNovelListPage> createState() => _TagNovelListPageState();
}

class _TagNovelListPageState extends State<TagNovelListPage> {
  @override
  void initState() {
    widget.presenter.presenterContext = context;
    widget.presenter.presenterSetState = setState;
    Future.delayed(Duration.zero)
        .then((value) => ModalRoute.of(context)?.settings.arguments as Classify)
        .then((value) {
      widget.controller.getNovelListByClassifyIds(int.parse(value.classifyId));
      return value;
    }).then((value) {
      widget.presenter.classify = value;
      widget.presenter.onComplete();
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: widget.presenter.classify == null
            ? const Text(I18n.loading)
            : Text(widget.presenter.classify!.classifyName),
      ),
      body: Column(
        children: [
          Padding(
            padding: const EdgeInsets.only(top: 8.0),
            child: ChipFilter(
                tight: true,
                onItemSelected: (value) {
                  widget.presenter.wordCountType = value.value;
                  widget.controller.getNovelListByClassifyIds(
                      int.parse(widget.presenter.classify!.classifyId));
                },
                selections: const {
                  ChipFilterEntry('全部', null),
                  ChipFilterEntry('100万字以下', 1),
                  ChipFilterEntry('100万-300万', 2),
                  ChipFilterEntry('300万以上', 3),
                }),
          ),
          ChipFilter(
              tight: true,
              onItemSelected: (value) {
                widget.presenter.bookStatusType = value.value;
                widget.controller.getNovelListByClassifyIds(
                    int.parse(widget.presenter.classify!.classifyId));
              },
              selections: const {
                ChipFilterEntry('全部', null),
                ChipFilterEntry('完结', 1),
                ChipFilterEntry('连载中', 2),
              }),
          ChipFilter(
              tight: true,
              onItemSelected: (value) {
                widget.presenter.sortType = value.value;
                widget.controller.getNovelListByClassifyIds(
                    int.parse(widget.presenter.classify!.classifyId));
              },
              selections: const {
                ChipFilterEntry('全部', null),
                ChipFilterEntry('热门', 1),
                ChipFilterEntry('最新', 2),
                ChipFilterEntry('评分', 3),
              }),
          Expanded(
            child: ListView(
                children: widget.presenter.novelList
                    .map((e) => NovelListTile(novel: e))
                    .toList()),
          ),
        ],
      ),
    );
  }
}
